﻿using System;
using System.Collections.Generic;

namespace ServerFramework.ORM
{
    public class DataCacheProxy
    {
        private static DataEntityCache GetCache<T>()
        {
            Type type = typeof(T);
            if(type.BaseType == typeof(PersonEntity))
            {
                return DbEntityManager.Instance.PersonCache;
            }
            else
            {
                return DbEntityManager.Instance.ShareCache;
            }
        }
        /// <summary>
        /// 找一条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="id"></param>
        /// <returns></returns>
        public static T FindKey<T>(long id) where T : DataEntityBase, new()
        {
            return GetCache<T>().FindKey<T>(id);
        }

        /// <summary>
        /// 找一组数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static List<T> FindKeys<T>(List<long> ids) where T : DataEntityBase, new()
        {
            return GetCache<T>().FindKeys<T>(ids);
        }

        /// <summary>
        /// 找所有数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="ids"></param>
        /// <returns></returns>
        public static List<T> FindAll<T>() where T : DataEntityBase, new()
        {
            DataEntityCache dec = GetCache<T>();
            return dec.FindAll<T>();
        }

        public static List<T> FindAllNoCache<T>() where T : DataEntityBase, new()
        {
            return GetCache<T>().ReflushAll<T>().FindAll<T>();
        }

        /// <summary>
        /// 条件查找
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="match"></param>
        /// <returns></returns>
        public static List<T> FindAll<T>(Predicate<T> match) where T : DataEntityBase, new()
        {
            return GetCache<T>().FindAll<T>(match);
        }

        /// <summary>
        /// 条件查找
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="match"></param>
        /// <returns></returns>
        public static List<T> FindAllNoCache<T>(Predicate<T> match) where T : DataEntityBase, new()
        {
            return GetCache<T>().ReflushAll<T>().FindAll<T>(match);
        }

        /// <summary>
        /// 加一条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public static void AddEntity<T>(T t) where T : DataEntityBase, new()
        {
            GetCache<T>().AddEntity<T>(t);
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public static void RemoveEntity<T>(T t) where T : DataEntityBase, new()
        {
            GetCache<T>().RemoveEntity<T>(t);
        }


        /// <summary>
        /// 删除一组数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public static void RemoveEntitys<T>(List<T> ts) where T : DataEntityBase, new()
        {
            GetCache<T>().RemoveEntitys<T>(ts);
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public static void Set<T>(T t) where T : DataEntityBase, new()
        {
            GetCache<T>().Set<T>(t);
        }

        /// <summary>
        /// 移除所有数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="t"></param>
        public static void RemoveAll<T>() where T : DataEntityBase, new()
        {
            GetCache<T>().RemoveAll<T>();
        }

        /// <summary>
        /// 条件移除
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="match"></param>
        public static void RemoveAll<T>(Predicate<T> match) where T : DataEntityBase, new()
        {
            GetCache<T>().RemoveAll<T>(match);
        }


        /// <summary>
        /// 获取所有的keys
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static List<long> GetKeys<T>() where T : DataEntityBase
        {
            return GetCache<T>().GetKeys<T>();
        }


        /// <summary>
        /// 获取某页的数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<T> GetPageList<T>(int pageIndex, int count) where T : DataEntityBase
        {
            return GetCache<T>().GetPageList<T>(pageIndex,count);
        }
    }
}
